package com.xy.net.refresh.support.utils;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.webkit.WebView;
import android.widget.AbsListView;
import android.widget.ScrollView;

import com.xy.net.refresh.support.impl.Pullable;

/**
 * Created by John on 2016/11/7.
 */

public class CanPullUtil {

    public CanPullUtil() {

    }

    public static Pullable getPullAble(View view) {
        return (Pullable) (view == null ? null
                : (view instanceof Pullable ? (Pullable) view
                : (view instanceof WebView ? new CanPullUtil.WebViewCanPull((WebView) view)
                : (view instanceof AbsListView ? new CanPullUtil.AbsListViewCanPull((AbsListView) view)
                : (view instanceof RecyclerView ? new CanPullUtil.RecyclerCanPull((RecyclerView) view)
                : new CanPullUtil.ScrollViewCanPull((ScrollView) view))))));
    }

    private static class WebViewCanPull implements Pullable {

        WebView webView;

        public WebViewCanPull(WebView webView) {
            this.webView = webView;
        }

        @Override
        public boolean isGetTop() {
            return webView.getVisibility() == View.GONE ? true : this.webView.getScrollY() <= 0;
        }

        @Override
        public boolean isGetBottom() {
            //getScrollY view可见区域顶端距界面顶端的位置距离。
            //WebView 有缩放功能, 所以当前整个页面的高度实际上应该是原始html 的高度再乘上缩放比例
            //getScrollY 向下为负 向上为正
            boolean isGet = (float) webView.getScrollY() >= (float) webView.getContentHeight() * webView.getScale() - webView.getMeasuredHeight();
            return webView.getVisibility() == View.GONE ? true : isGet;
        }
    }

    private static class ScrollViewCanPull implements Pullable {

        ScrollView scrollView;

        public ScrollViewCanPull(ScrollView scrollView) {
            this.scrollView = scrollView;
        }

        @Override
        public boolean isGetTop() {
            return scrollView.getVisibility() == View.GONE ? true : scrollView.getScrollY() <= 0;
        }

        @Override
        public boolean isGetBottom() {
            return this.scrollView.getVisibility() == View.GONE ? true : this.scrollView.getChildCount() <= 0 || this.scrollView.getScrollY() >= this.scrollView.getChildAt(0).getHeight() - this.scrollView.getMeasuredHeight();
        }
    }

    private static class AbsListViewCanPull implements Pullable {
        AbsListView absListView;

        public AbsListViewCanPull(AbsListView absListView) {
            this.absListView = absListView;
        }

        public boolean isGetTop() {
            return this.absListView.getCount() == 0 ? true : this.absListView.getFirstVisiblePosition() == 0 && (this.absListView.getChildCount() <= 0 || this.absListView.getChildAt(0).getTop() >= this.absListView.getPaddingTop());
        }

        @Override
        public boolean isGetBottom() {
            int firstVisiblePosition = this.absListView.getFirstVisiblePosition();
            int lastVisiblePosition = this.absListView.getLastVisiblePosition();
            int count = this.absListView.getCount();
            if (count == 0) {
                return true;
            } else {
                if (lastVisiblePosition == count - 1) {
                    View view = this.absListView.getChildAt(lastVisiblePosition - firstVisiblePosition);
                    if (view == null || view.getBottom() <= this.absListView.getHeight() - this.absListView.getMeasuredHeight()) {
                        return true;
                    }
                }
            }
            return false;
        }
    }

    private static class RecyclerCanPull implements Pullable {

        private RecyclerView recyclerView;
        private LinearLayoutManager layoutManager;

        public RecyclerCanPull(RecyclerView recyclerView) {
            this.recyclerView = recyclerView;
        }

        private void initLayoutManager() {
            if (this.layoutManager == null) {
                RecyclerView.LayoutManager layout = this.recyclerView.getLayoutManager();
                if (layout != null && layout instanceof LinearLayoutManager) {
                    layoutManager = (LinearLayoutManager) layout;
                }
            }
        }

        @Override
        public boolean isGetTop() {
            initLayoutManager();
            if (layoutManager != null) {
                if (recyclerView.getVisibility() == View.GONE) {
                    return true;
                }
                if (this.layoutManager.getItemCount() == 0) {
                    return true;
                }
                if (this.layoutManager.findFirstVisibleItemPosition() == 0 && (this.recyclerView.getChildCount() <= 0 || this.recyclerView.getTop() >= this.recyclerView.getPaddingTop())) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public boolean isGetBottom() {
            this.initLayoutManager();
            if (layoutManager != null) {
                if (layoutManager.getItemCount() == 0) {
                    return true;
                }
                if (recyclerView.getVisibility() == View.GONE) {
                    return true;
                }
                if (layoutManager.findLastVisibleItemPosition() == layoutManager.getItemCount() - 1) {
                    return true;
                }
            }
            return false;
        }
    }
}
